package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {
    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private IStatementService statementService;
    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(),qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void save(Appointment appointment) {
        appointment.setRemoveStatus(Appointment.REMOVE_ZERO);
        appointmentMapper.insert(appointment);
    }

    @Override
    public void update(Appointment appointment) {
        Appointment Oldappointment = this.get(appointment.getId());
        if (!Appointment.STATUS_APPOINTMENT.equals(Oldappointment.getStatus())){
            throw new BusinessException("只有状态为预约中的预约单才能进行编辑操作");
        }
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] Ids = Convert.toLongArray(ids);
        for (Long id : Ids) {
            appointmentMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

    @Override
    public void arrivalById(String id) {
        appointmentMapper.ById(id,Appointment.STATUS_ARRIVAL);
    }

    @Override
    public void cancelById(String id) {
        appointmentMapper.ById(id,Appointment.STATUS_CANCEL);
    }

    @Override
    public void deleteById(String id) {
        appointmentMapper.deleteById(id,Appointment.REMOVE_ONE);
    }

    @Override
    public Statement generateStatement(Long appointmentId) {
        Appointment appointment = this.get(appointmentId);
        if (!(Appointment.STATUS_ARRIVAL.equals(appointment.getStatus())
              ||Appointment.STATUS_SETTLE.equals(appointment.getStatus()))){
            throw new BusinessException("只有处于已到店和已结算状态才能进行结算单处理");
        }
        Statement statement = statementService.getByAppointmentId(appointmentId);
        if (statement==null){
            statement = new Statement();
            statement.setAppointmentId(appointmentId);
            statement.setCarSeries(appointment.getCarSeries());
            statement.setActualArrivalTime(appointment.getActualArrivalTime());
            statement.setCreateTime(new Date());
            statement.setCustomerName(appointment.getCustomerName());
            statement.setCustomerPhone(appointment.getCustomerPhone());
            statement.setInfo(appointment.getInfo());
            statement.setServiceType(appointment.getServiceType());
            statement.setLicensePlate(appointment.getLicensePlate());
            statementService.save(statement);
        }
        return statement;
    }

    @Override
    public void updateStatus(Long appointmentId, Integer statusSettle) {
        appointmentMapper.updateStatus(appointmentId,statusSettle);
    }

}
